/**
 *
 * @param obj 一个proxy数据对象
 * @param path 对象路径 例如："box.position.x"
 * @returns 数据对象在对象路径上的值 例如：box['position']['x'];
 */

let getGoalData = function (obj: object, path: string) {
  // 如果开头存在引号，表明，需要获取的值并不是一个变量，而是path这段字符串
  // ！！！！！！！！！！！！不应该在这处理
  // 也就不需要在obj上查找
  let goal: any = obj;
  if (path[0] == "'" || path[0] == '"') {
    goal = path.replace(/[\"\']/g, "");
    return goal;
  }
  let _path = path.trim().split(".");
  _path.forEach((item) => {
    if (item.length > 0) {
      goal = goal[item];
    }
  });
  return goal;
};

function setGoalData(obj: object, path: string, val: any) {
  let goal: any = obj;
  let _path = path.trim().split('.');
  let key = <string>_path.pop();
  _path.forEach(item => {
    if (item.length > 0) {
      goal = goal[item];
    }
  })
  goal[key] = val;
}

export { getGoalData, setGoalData };
